package com.lafengmaker.controller;

import com.lafengmaker.vo.ResponseDTO;
import com.lafengmaker.vo.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * @author lafengmaker
 * @title: HelloController
 * @projectName demo
 * @description: TODO
 * @email yeweijun@souche.com
 * @date 1/21/21 9:48 AM
 */
@Controller
public class HelloController {
    @GetMapping("/hi")
    @ResponseBody
    public ResponseDTO<String> sayHi(@Valid User user, BindingResult result) {
        StringBuilder sb = new StringBuilder();
        if (result.hasErrors()) {
            result.getAllErrors().stream().forEach(e -> sb.append(e.getDefaultMessage()));
            throw new IllegalArgumentException(sb.toString());
        }
        return ResponseDTO.success(user);
    }

    @RequestMapping("/test")
    public String test(Model model, @RequestParam(value = "reqName", required = false) String name) {
        model.addAttribute("text", "hello world!!!," + name);
        model.addAttribute("testdata", "new test data");
        return "index";
    }
}
